package top.carrotguo.weibo;

import android.content.Intent;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.orhanobut.logger.Logger;
import com.sina.weibo.sdk.WbSdk;
import com.sina.weibo.sdk.auth.AccessTokenKeeper;
import com.sina.weibo.sdk.auth.AuthInfo;
import com.sina.weibo.sdk.auth.Oauth2AccessToken;
import com.sina.weibo.sdk.auth.WbConnectErrorMessage;
import com.sina.weibo.sdk.auth.sso.SsoHandler;

import java.sql.RowId;

import butterknife.BindInt;
import butterknife.BindView;
import butterknife.OnClick;
import top.carrotguo.weibo.contract.MainContract;
import top.carrotguo.weibo.presenter.MainPresenter;
import top.carrotguo.weibo.ui.base.MVPBaseActivity;
import top.carrotguo.weibo.ui.focus.FocusFragment;
import top.carrotguo.weibo.ui.home.HomeFragment;
import top.carrotguo.weibo.ui.message.MessageFragment;
import top.carrotguo.weibo.ui.person.PersonFragment;
import top.carrotguo.weibo.utils.Constants;

public class MainActivity extends MVPBaseActivity<MainContract.View,MainPresenter>
    implements MainContract.View{

    @BindView(R.id.fl_content)
    FrameLayout flContent;
    @BindView(R.id.iv_home)
    ImageView ivHome;
    @BindView(R.id.iv_message)
    ImageView ivMessage;
    @BindView(R.id.iv_focus)
    ImageView ivFocus;
    @BindView(R.id.iv_person)
    ImageView ivPerson;
    @BindView(R.id.tv_home)
    TextView tvHome;
    @BindView(R.id.tv_message)
    TextView tvMessage;
    @BindView(R.id.tv_focus)
    TextView tvFocus;
    @BindView(R.id.tv_person)
    TextView tvPerson;

    private HomeFragment homeFragment;
    private MessageFragment messageFragment;
    private FocusFragment focusFragment;
    private PersonFragment personFragment;
    private FragmentManager fm;

    @Override
    protected void initView() {
        super.initView();
        fm = getSupportFragmentManager();
        //先显示首页
        setCurrentPage(0);
    }

    /**
     * 设置当前的fragment
     * @param index
     */
    private void setCurrentPage(int index){
        //每个ft只能执行一次commit
        FragmentTransaction ft = fm.beginTransaction();
        //先使所有button恢复未选中状态
        showNormalIcon();
        //先隐藏所有的fragment
        hideAllPage(ft);
        switch (index) {
            case 0:{
                //更新图片
                ivHome.setBackgroundResource(R.drawable.icon_home_select);
                if (homeFragment == null) {
                    //若fragment不存在，则先添加到viewgroup中再显示
                    homeFragment = new HomeFragment();
                    ft.add(R.id.fl_content,homeFragment);
                } else {
                    //viewgroup中已经存在该视图，直接显示视图
                    ft.show(homeFragment);
                }
                break;
            }
            case 1:{
                ivMessage.setBackgroundResource(R.drawable.icon_message_select);
                if (messageFragment == null) {
                    messageFragment = new MessageFragment();
                    ft.add(R.id.fl_content,messageFragment);
                } else {
                    ft.show(messageFragment);
                }
                break;
            }
            case 2:{
                ivFocus.setBackgroundResource(R.drawable.icon_focus_select);
                if (focusFragment == null) {
                    focusFragment = new FocusFragment();
                    ft.add(R.id.fl_content,focusFragment);
                } else {
                    ft.show(focusFragment);
                }
                break;
            }
            case 3:{
                ivPerson.setBackgroundResource(R.drawable.icon_person_select);
                if (personFragment == null) {
                    personFragment = new PersonFragment();
                    ft.add(R.id.fl_content,personFragment);
                } else {
                    ft.show(personFragment);
                }
                break;
            }
        }
        //提交事物
        ft.commit();
    }

    /**
     * 设置所有按钮为未选中状态
     */
    private void showNormalIcon(){
        ivHome.setBackgroundResource(R.drawable.icon_home_normal);
        ivMessage.setBackgroundResource(R.drawable.icon_message_normal);
        ivFocus.setBackgroundResource(R.drawable.icon_focus_normal);
        ivPerson.setBackgroundResource(R.drawable.icon_person);
    }

    /**
     * 隐藏所有页面
     */
    private void hideAllPage(FragmentTransaction ft){
        if (homeFragment != null) {
            ft.hide(homeFragment);
        }
        if (messageFragment != null) {
            ft.hide(messageFragment);
        }
        if (focusFragment != null) {
            ft.hide(focusFragment);
        }
        if (personFragment != null) {
            ft.hide(personFragment);
        }
    }

    @OnClick({R.id.ll_home,R.id.ll_message,R.id.ll_focus,R.id.ll_person})
    public void changeFragment(View view){
        switch (view.getId()){
            case R.id.ll_home:
                setCurrentPage(0);
                break;
            case R.id.ll_message:
                setCurrentPage(1);
                break;
            case R.id.ll_focus:
                setCurrentPage(2);
                break;
            case R.id.ll_person:
                setCurrentPage(3);
                break;
        }
    }

    @Override
    public int provideContentViewId() {
        return R.layout.activity_main;
    }

    @Override
    public Bundle getBundleExtras(Bundle bundle) {
        return null;
    }

    @Override
    public MainPresenter createPresenter() {
        return new MainPresenter();
    }


}
